<?php

namespace App\Model;


/**
 * Class Commodity_info
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/24 0024
 * @Time 20:36
 */
/**
 * Class Commodity_info
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/29 0029
 * @Time 20:38
 */
/**
 * Class Commodity_info
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/30 0030
 * @Time 10:47
 */
class Commodity_info extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = "commodity_info_id";
    /**
     * @var string
     */
    protected $table = "commodity_infos";

    /**
     * 方法 : add_info
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param $var
     * @return bool
     */
    public function add_info($var)
    {
        $this->commodity_id = $var["commodity_id"];
        $this->commodity_classify_id = $var["commodity_classify_id"];
        $this->commodity_size_id = $var["commodity_size_id"];
        $this->commodity_color_id = $var["commodity_color_id"];
        $this->commodity_info_number = $var["commodity_info_number"];
        $this->commodity_info_price = $var["commodity_info_price"];
        $this->info_img_url = $var["info_img_url"];
        if (!$this->save())
            return false;
        return true;

        $where = array(
            array(
                "where", "commodity_info_id", "=", $this->commodity_info_id
            )
        );
        $data = $this->get($where, "*", 0, 0);
        if (empty($data)) {
//            $this->set_empty_array_redis("commodity_info:" . $this->commodity_info_id . ":");
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis("commodity_info:" . $this->commodity_info_id . ":*"))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis("commodity_info:" . $value["commodity_info_id"] . ":commodity:" . $value["commodity_id"] . ":color:" . $value["commodity_color_id"] . ":size:" . $value["commodity_size_id"], $value);
        }
    }

    /**
     * 方法 : update_info
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param $var
     * @return bool
     */
    public function update_info($var)
    {
        $this->commodity_classify_id = $var["commodity_classify_id"];
        $this->commodity_size_id = $var["commodity_size_id"];
        $this->commodity_color_id = $var["commodity_color_id"];
        $this->commodity_info_number = $var["commodity_info_number"];
        $this->commodity_info_price = $var["commodity_info_price"];
        $this->info_img_url = $var["info_img_url"];
        if (!$this->save())
            return false;
        $where = array(
            array(
                "where", "commodity_info_id", "=", $this->commodity_info_id
            )
        );
        $data = $this->get($where, "*", 0, 0);
        if (empty($data)) {
            $this->set_empty_array_redis("commodity_info:" . $this->commodity_info_id . ":");
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis("commodity_info:" . $this->commodity_info_id . ":*"))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : get_all_info
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @return bool|mixed
     */
    public function get_all_info()
    {
        $key_index = "commodity_info:*";
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), "*", 0, 0, ["commodity_id", "asc"]);
            if (empty($data))
                $this->set_empty_array_redis("commodity_info:0");
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_info_by_commodity_id
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param $commodity_id
     * @return bool|mixed
     */
    public function get_info_by_commodity_id($commodity_id)
    {
        $key_index = "commodity_info:*:commodity:" . $commodity_id . ":color:*:size:*";
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    "where", "commodity_id", "=", $commodity_id
                )
            );
            $data = $this->get($where, "*", 0, 0);
            if (empty($data))
                $this->set_empty_array_redis("commodity_info:0:commodity:" . $commodity_id . ":color:0:size:0");
            $this->set_array($data);
        }
//        $keys = $this->get_info_keys_by_commodity($commodity_id);
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_info_keys_by_commodity
     * @User wuqin
     * @DateTime 2018-03-30 10:47
     * @param $commodity_id
     * @return mixed
     */
    public function get_info_keys_by_commodity($commodity_id)
    {
        $key_index = "info_keys_by_commodity:" . $commodity_id;
        if (empty($keys = $this->exists_redis($key_index))) {

            $this->set_array_redis($key_index, $this->keys_redis("commodity_info:*:commodity:" . $commodity_id . ":color:*:size:*"));
        }
        $data = $this->mget_array_redis($key_index);
        return reset($data);
    }
    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-24 20:36
     * @param string $commodity_info_id
     * @param string $commodity_id
     */
    public function set_empty($commodity_info_id = "0", $commodity_id = "0", $color = "0", $size = "0")
    {
        $this->set_empty_array_redis("commodity_info:" . $commodity_info_id . ":commodity:" . $commodity_id . ":color:" . $color . ":size:" . $size);
    }
}
